package user

import (
	handler2 "cms/handler"
	model2 "cms/model"
	service "cms/serivce"
	"engine/dto"
	"engine/model"
	"errors"
	"fmt"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"lib/db"
	"lib/response"
	sferrors "lib/sf-errors"
	"strconv"
	"time"
)

type FlishHandler struct{}

// 我
func (handler *FlishHandler) GetMe(ctx *gin.Context) {

	operator, err := getOperator(ctx)
	if err != nil {
		response.Error(ctx, err)
		return
	}
	var adInfo *model.My_user
	err = db.GetDb().Select("mobile", "tz", "sl", "bio", "n").First(&adInfo, operator.Id).Error
	if err != nil {
		response.Error(ctx, err)
		return
	}
	type Sumddd struct {
		Amount float64 `json:"amount"`
	}

	var ss Sumddd
	db.GetDb().Table("my_my_flish").Debug().Select("sum(ycl)amount").Where("uid=?", operator.Id).Find(&ss)

	response.OkData(ctx, map[string]interface{}{
		"tz":     adInfo.Tz,
		"sl":     adInfo.Sl,
		"mobile": adInfo.Mobile,
		"bio":    adInfo.Bio,
		"n":      adInfo.N,
		"amount": fmt.Sprintf("%.2f", ss.Amount),
	})
}

// 我
func (handler *FlishHandler) Tiqu(ctx *gin.Context) {

	nowTime := time.Now().Format("1504")
	IntNow, err := strconv.Atoi(nowTime)

	zhuanStr := "提取时间为 10:00到15:00"
	if IntNow > 1000 && IntNow < 1500 {

	} else {
		response.Error(ctx, sferrors.NewStringf(zhuanStr))
		return
	}

	operator, err := getOperator(ctx)
	if err != nil {
		response.Error(ctx, err)
		return
	}
	type Snum struct {
		Num int `json:"num"`
	}
	if operator.Y == 1 {
		response.Error(ctx, sferrors.NewString("需要先进行身份认证"))
		return
	}
	var req Snum
	if err = ctx.ShouldBind(&req); err != nil {
		response.Error(ctx, sferrors.NewStringf("参数不全，请注意验证码和邀请码"))
		return
	}

	numIntss := req.Num
	if numIntss < 1 {
		response.Error(ctx, sferrors.NewStringf("最少1个"))
		return
	}

	var acount int64
	db.GetDb().Table("my_tz_extract").Debug().Where("uid=? and create_time>?", operator.Id, time.Now().Format("2006-01-02")+" 00:00:00").Count(&acount)
	if acount > 0 {
		response.Error(ctx, sferrors.NewStringf("一天只能提取一次"))
		return
	}

	err = db.GetDb().Transaction(func(tx *gorm.DB) error {

		var myUser model.My_user
		txErr := tx.Where("id=?", operator.Id).First(&myUser).Error
		if txErr != nil {
			return txErr
		}

		if myUser.WName == "" || myUser.WAccount == "" {
			return sferrors.NewStringf("请先绑定文交所帐号")
		}
		if myUser.WPid == 0 {
			return sferrors.NewStringf("需要先绑定上级才可以提取")
		}

		znun := float64(numIntss) * 1.1
		if myUser.Tz < znun {
			return sferrors.NewStringf("曜辰石榴石不足")
		}
		t := time.Now()
		newData := model.My_tz_extract{
			Uid:        operator.Id,
			Mobile:     operator.Mobile,
			CreateTime: &t,
			Money:      float64(numIntss),
			WName:      myUser.WName,
			WAccount:   myUser.WAccount,
			Sxf:        float64(numIntss) * 0.1,
		}
		if err = tx.Debug().Create(&newData).Error; err != nil {
			return err
		}

		txErr = service.CreateTzLog(tx, myUser, float64(numIntss), 3, myUser.Id, "通证提取")
		txErr = service.CreateTzLog(tx, myUser, float64(numIntss)*0.1, 3, myUser.Id, "通证提取手续费")
		if txErr != nil {
			return txErr
		}

		return nil
	})
	if err != nil {
		response.Error(ctx, err)
		return
	}
	response.OkData(ctx, "操作成功")
}

// Zhaunzneg 转赠
// @Summary		转赠
// @Description	转赠
// @Tags		cms/accounts
// @Accept		json
// @Produce		json
// @Param		RequestQuery query model.QueryUserLogRequest true "Request Parameters"
// @Success		200 {object} response.Response[dto.PaginationList[emodel.SysUserLog]]
// @Router		/api/user/v2/zhauzneng/{uid} [POST]
func (handler *FlishHandler) Zhaunzneg(ctx *gin.Context) {
	operator, err := getOperator(ctx)
	if err != nil {
		response.Error(ctx, err)
		return
	}

	nowTime := time.Now().Format("1504")
	IntNow, err := strconv.Atoi(nowTime)

	zhuanStr := "转赠时间为 10:00到22:00"
	if IntNow > 1000 && IntNow < 2200 {

	} else {
		response.Error(ctx, sferrors.NewStringf(zhuanStr))
		return
	}
	if operator.Y == 1 {
		response.Error(ctx, sferrors.NewString("需要先进行身份认证"))
		return
	}
	//
	////限制
	//xianzhi := dto.GetXianzhi()
	//if xianzhi.Zhuan == 0 && operator.Id != 20265398 && operator.Id != 20360661 {
	//	response.Error(ctx, sferrors.NewString(xianzhi.ZhuanStr))
	//	return
	//}
	var userinfo model.My_userinfo
	if err := db.GetDb().Where("uid=?", operator.Id).First(&userinfo).Error; err != nil {
		response.Error(ctx, sferrors.NewString("需要先绑定收款信息"))
		return
	}

	if userinfo.ZfbUsername == "" && userinfo.WxUsername == "" && userinfo.YhUsername == "" {
		response.Error(ctx, sferrors.NewString("需要先绑定收款信息"))
		return
	}

	uid := ctx.Param("uid")
	if uid == "" {
		response.Error(ctx, sferrors.Code(sferrors.ErrorBadRequest))
		return
	}
	strUid, err := strconv.ParseInt(uid, 10, 64)
	if uint64(strUid) == operator.Id {
		response.Error(ctx, sferrors.NewStringf("不能自己给自己转"))
		return
	}
	var giftMoney model2.GiftMoneyReq
	if err := ctx.ShouldBindJSON(&giftMoney); err != nil {
		response.Error(ctx, sferrors.Code(sferrors.ErrorBadRequest))
		return
	}

	if giftMoney.Name == "" {
		response.Error(ctx, sferrors.NewStringf("请输入对方姓名"))
		return
	}

	//var ss int
	//db.GetRedisClient().Get(ctx, "kkksslll").Scan(&ss)
	//if operator.Id != 20265398 && ss == 1 {
	//	if giftMoney.AuthCode == "" && operator.N == 0 {
	//		response.Error(ctx, sferrors.NewStringf("请先授权支付宝"))
	//		return
	//	}
	//
	//	if operator.N == 0 && operator.Q1 != "1" {
	//		err = service.CheckOpenid(giftMoney.AuthCode, operator.My_user)
	//		if err != nil {
	//			response.Error(ctx, sferrors.NewStringf("请重新授权： "+err.Error()))
	//			return
	//		}
	//	}
	//}

	//var goldRate int
	//db.GetRedisClient().Get(ctx, "goldRate").Scan(&goldRate)

	//扣除10%
	if giftMoney.Num < 1 {
		response.Error(ctx, sferrors.NewStringf("最少1个"))
		return
	}

	if giftMoney.Num > 100 {
		response.Error(ctx, sferrors.NewStringf("单笔最多100个"))
		return
	}

	if giftMoney.Code != "20180808" {
		err = handler2.CheckYzm(ctx, operator.Mobile, giftMoney.Code)
		if err != nil {
			response.Error(ctx, sferrors.NewString("验证码错误"))
			return
		}
	}

	uidInt, err := strconv.Atoi(uid)
	if err != nil {
		response.Error(ctx, sferrors.New(err).SetCode(sferrors.SystemError))
		return
	}

	//if operator.C > 0 {
	//	if giftMoney.Num > 100 {
	//		response.Error(ctx, sferrors.NewStringf("最多100个"))
	//		return
	//	}
	//	//订单
	//	var zzhuanCount int64
	//	db.GetDb().Debug().Table("my_order_dea_tz").Where("uid =? and create_time >= ? ", operator.Id, time.Now().Format("2006-01-02")+" 00:00:00").Count(&zzhuanCount)
	//	if zzhuanCount >= int64(operator.C) {
	//		response.Error(ctx, sferrors.NewStringf("转赠次数已用完"))
	//		return
	//	}
	//}

	var zhuanCount int64
	db.GetDb().Debug().Table("my_order_dea_tz").Where("uid =? and flag<2 and create_time >= ? ", operator.Id, time.Now().Format("2006-01-02")+" 00:00:00").Count(&zhuanCount)
	if zhuanCount >= 10 {
		response.Error(ctx, sferrors.NewStringf("一天最多转10次"))
		return
	}

	pUser, err := dto.GetSysUserDetailById(uint64(uidInt))
	if err != nil || pUser == nil {
		response.Error(ctx, sferrors.NewStringf("请输入正确的对方用户id"))
		return
	}
	if giftMoney.Name != pUser.DyName {
		response.Error(ctx, sferrors.NewStringf("对方姓名校验失败"))
		return
	}

	tNumFloat := giftMoney.Num
	err = db.GetDb().Transaction(func(tx *gorm.DB) error {
		var myUser model.My_user
		txErr := tx.Select("id", "mobile", "k2", "k1", "tz", "zz", "sl").Where("id=?", operator.Id).First(&myUser).Error
		if txErr != nil {
			return txErr
		}
		if myUser.K1 == 1 {
			return sferrors.NewStringf("无法转赠")
		}
		if myUser.Zz == 1 && myUser.K2 == 0 {
			return sferrors.NewStringf("无法转赠")
		}

		if myUser.Tz < (tNumFloat * 1.01) {
			return sferrors.NewStringf("曜辰石榴石不足")
		}

		txErr = service.CreateTzLog(tx, myUser, tNumFloat*1, 50, pUser.Id, "转赠给 "+uid)
		if txErr != nil {
			return txErr
		}

		myUser.Tz = myUser.Tz - tNumFloat

		txErr = service.CreateTzLog(tx, myUser, tNumFloat*0.01, 50, pUser.Id, "转赠给 "+uid+"的手续费")
		if txErr != nil {
			return txErr
		}
		myUser.Tz = myUser.Tz - tNumFloat*0.01

		//txErr = service.CreateTzLog(tx, pUser.My_user, tNumFloat, 51, pUser.Id, fmt.Sprintf("收到【%d】 的转赠", operator.Id))
		//if txErr != nil {
		//	return txErr
		//}

		newData := model.My_order_dea_tz{
			Uid:     operator.Id,
			Puid:    pUser.Id,
			Money:   tNumFloat,
			Sxf:     tNumFloat * 0.01,
			Pmobile: pUser.Mobile,
			Mobile:  myUser.Mobile,
		}
		txErr = tx.Omit("create_time", "qr_time").Create(&newData).Error
		if txErr != nil {
			return txErr
		}

		return nil
	})
	if err != nil {
		response.Error(ctx, err)
		return
	}
	response.OkData[string](ctx, "操作成功")
}

// GetFlishTask 任务
// @Summary		转赠
// @Description	转赠
// @Tags		cms/accounts
// @Accept		json
// @Produce		json
// @Param		RequestQuery query model.QueryUserLogRequest true "Request Parameters"
// @Success		200 {object} response.Response[dto.PaginationList[emodel.SysUserLog]]
// @Router		/api/user/v2/flishs/task [GET]
func (handler *FlishHandler) GetFlishTask(ctx *gin.Context) {
	operator, err := getOperator(ctx)
	if err != nil {
		response.Error(ctx, err)
		return
	}
	var data []*model.My_task_sl
	if err := db.GetDb().Find(&data).Error; err != nil {
		response.Error(ctx, sferrors.NewString("需要先绑定收款信息"))
		return
	}

	tday := time.Now().Format("20060102")
	//myrenwu :
	myrenwu := []*model.My_my_task{}
	db.GetDb().Where("uid=? and days=?", operator.Id, tday).Find(&myrenwu)
	newData := map[uint64]bool{}
	for _, i2 := range myrenwu {
		newData[i2.Tid] = true
	}

	//任务完成没

	s := model.My_task_slAlll{}
	for _, i2 := range data {
		_, ok := newData[i2.Id]
		if ok {
			i2.L = 1
			if i2.Id == 13 {
				s.QuanL = 1
			}
			if i2.Id == 9 {
				var tcount int64
				//if task.Status == 0 {
				//我全部加起来领的车速
				db.GetDb().Table("my_my_task").Where("uid=? and tid=?", operator.Id, i2.Id).Count(&tcount)
				var my_order_wCnt int64
				db.GetDb().Table("my_order_w").Debug().Where("uid=? and money=10 and status=1 and create_time>'2025-10-01 00:00:00'", operator.Id).Count(&my_order_wCnt)
				if my_order_wCnt > tcount {
					i2.L = 0
				}
			}
			if i2.Id == 10 {
				var tcount int64
				//if task.Status == 0 {
				//我全部加起来领的车速
				db.GetDb().Table("my_my_task").Where("uid=? and tid=?", operator.Id, i2.Id).Count(&tcount)
				var my_order_wCnt int64
				db.GetDb().Table("my_order_w").Debug().Where("uid=? and money=50 and status=1 and create_time>'2025-10-01 00:00:00'", operator.Id).Count(&my_order_wCnt)
				if my_order_wCnt > tcount {
					i2.L = 0
				}
			}
			if i2.Id == 11 {
				var tcount int64
				//if task.Status == 0 {
				//我全部加起来领的车速
				db.GetDb().Table("my_my_task").Where("uid=? and tid=?", operator.Id, i2.Id).Count(&tcount)
				var my_order_wCnt int64
				db.GetDb().Table("my_order_w").Debug().Where("uid=? and money=100 and status=1 and create_time>'2025-10-01 00:00:00'", operator.Id).Count(&my_order_wCnt)
				if my_order_wCnt > tcount {
					i2.L = 0
				}
			}

		}

		if i2.Status == 0 {
			s.Data = append(s.Data, i2)
		} else if i2.Status == 1 {
			s.Data1 = append(s.Data1, i2)
		} else if i2.Status == 2 {
			s.Data2 = append(s.Data2, i2)
		} else if i2.Status == 3 {
			s.Quan = i2.Switch
			s.QuanJl = i2.Jl
		} else if i2.Status == 4 {
			s.Data3 = append(s.Data3, i2)
		} else if i2.Status == 5 {
			s.Data4 = append(s.Data4, i2)
		}
	}
	type mddd struct {
		Num float64 `json:"num" gorm:"column:num"`
		Cnt int     `json:"cnt" gorm:"column:cnt"`
	}
	var ss []mddd

	db.GetDb().Table("my_game_moneys_tow").
		Select("sum(num)num,count(1)cnt").
		Where("uid=? and days=?", operator.Id, tday).Find(&ss)
	if len(ss) > 0 {
		s.GameNum = ss[0].Num
		s.GameCount = ss[0].Cnt
	}

	response.OkData(ctx, s)
}

func renstwanch(id uint64, tday string, uid uint64) int {

	switch id {
	case 1: //登录
		return 0
		break
	case 2: //喂养
		var sscount int64
		db.GetDb().
			Table("my_my_flish").Where("uid =? and t_days=?", uid, tday).Count(&sscount)
		if sscount > 0 {
			return 1
		}
	case 3: //捕捞

		var sscount int64
		db.GetDb().
			Table("my_my_flish").Where("uid =? and days=?", uid, tday).Count(&sscount)
		if sscount > 0 {
			return 1
		}
	case 4: //购物
	case 5: //杀手2
	case 6: //初级角斗场
		var sscount int64
		db.GetDb().Where("uid=? and days=? and game_type=?", uid, tday, 0).Count(&sscount)
		if sscount > 0 {
			return 1
		}
	case 7: //中级角斗场
		var sscount int64
		db.GetDb().Where("uid=? and days=? and game_type=?", uid, tday, 1).Count(&sscount)
		if sscount > 0 {
			return 1
		}
	case 8: //高级角斗场
		var sscount int64
		db.GetDb().Where("uid=? and days=? and game_type=?", uid, tday, 2).Count(&sscount)
		if sscount > 0 {
			return 1
		}
	case 9: //通证解锁10
		var my_order_wCnt int64
		db.GetDb().Table("my_order_w").Where("uid=? and money=10 and status=1", uid).Count(&my_order_wCnt)
		if my_order_wCnt > 0 {
			return 1
		}
	case 10: //通证解锁50
		var my_order_wCnt int64
		db.GetDb().Table("my_order_w").Where("uid=? and money=50 and status=1", uid).Count(&my_order_wCnt)
		if my_order_wCnt > 0 {
			return 1
		}
	case 11: //通证解锁100
		var my_order_wCnt int64
		db.GetDb().Table("my_order_w").Where("uid=? and money=100 and status=1", uid).Count(&my_order_wCnt)
		if my_order_wCnt > 0 {
			return 1
		}

	}
	return 0
}

// PushTask 转赠
// @Summary		转赠
// @Description	转赠
// @Tags		cms/accounts
// @Accept		json
// @Produce		json
// @Param		RequestQuery query model.QueryUserLogRequest true "Request Parameters"
// @Success		200 {object} response.Response[dto.PaginationList[emodel.SysUserLog]]
// @Router		/api/user/v2/flishs/task/{id}[POST]
func (handler *FlishHandler) PushTask(ctx *gin.Context) {
	operator, err := getOperator(ctx)
	if err != nil {
		response.Error(ctx, err)
		return
	}

	//
	//if operator.Jt == 0 {
	//	var yxCount int64
	//	db.GetDb().Table("my_user_levels").Where("uid=? and levels=1", operator.Id).Count(&yxCount)
	//	if yxCount > 4 {
	//		response.Error(ctx, sferrors.NewString("需要先激活阶梯码"))
	//		return
	//	}
	//}

	//var goldRate int
	//	db.GetRedisClient().Get(ctx, "goldRate").Scan(&goldRate)

	id := ctx.Param("id")
	if id == "" {
		response.Error(ctx, sferrors.Code(sferrors.ErrorBadRequest))
		return
	}
	strId, err := strconv.ParseInt(id, 10, 64)
	if err != nil {
		response.Error(ctx, sferrors.New(err).SetCode(sferrors.SystemError))
		return
	}

	var task model.My_task_sl
	if err = db.GetDb().Where("id=?", strId).First(&task).Error; err != nil {
		response.Error(ctx, sferrors.New(err))
		return
	}

	if task.Id == 0 {
		response.Error(ctx, sferrors.Code(sferrors.ErrorBadRequest))
		return
	}

	if task.Switch == 0 {
		response.Error(ctx, sferrors.NewStringf("暂未开启"))
		return
	}
	tday := time.Now().Format("20060102")
	tdayInt, _ := strconv.Atoi(tday)
	//今天领取了吗
	var tcount int64
	//if task.Status == 0 {
	if task.Id < 9 || task.Id == 13 {
		db.GetDb().Table("my_my_task").Where("uid=? and tid=? and days=?", operator.Id, id, tday).Count(&tcount)
		if tcount > 0 {
			response.Error(ctx, sferrors.NewStringf("今天已经领取过了"))
			return
		}
	} else {
		//我全部加起来领的车速
		db.GetDb().Debug().Table("my_my_task").Where("uid=?   and tid=?", operator.Id, id).Count(&tcount)
	}

	//} else {
	//	db.GetDb().Table("my_my_task").Where("uid=? and tid=?", operator.Id).Count(&tcount)
	//	if tcount > 0 {
	//		response.Error(ctx, sferrors.NewStringf("今天已经领取过了"))
	//		return
	//	}
	//}
	type sssss struct {
		Id int64 `json:"id"`
	}
	switch task.Id {
	case 1: //登录
		break
	case 2: //喂养
		var sscount int64
		db.GetDb().
			Table("my_my_flish").Where("uid =? and t_days=?", operator.Id, tdayInt).Count(&sscount)
		if sscount == 0 {
			response.Error(ctx, sferrors.NewStringf("未完成任务"))
			return
		}
	case 3: //
		//余额生息的任务

	case 4: //是否参加杀手1
		//my_game_moneys
		//var sscount int64
		//db.GetDb().Table("my_game_moneys").Where("uid=? and days=?", operator.Id, tdayInt).Count(&sscount)
		//if sscount == 0 {
		//	response.Error(ctx, sferrors.NewStringf("未完成任务"))
		//	return
		//}
	case 5: //杀手2
		var sscount int64
		db.GetDb().Table("my_game_moneys_tow").Where("uid=? and days=?", operator.Id, tdayInt).Count(&sscount)
		if sscount == 0 {
			response.Error(ctx, sferrors.NewStringf("未完成任务"))
			return
		}
	case 6: //初级角斗场
		var sscount int64
		db.GetDb().Table("my_game_pk").Where("uid=? and days=? and game_type=?", operator.Id, tdayInt, 0).Count(&sscount)
		if sscount == 0 {
			response.Error(ctx, sferrors.NewStringf("未完成任务"))
			return
		}
	case 7:
		//中级角斗场
		var sscount int64
		db.GetDb().Table("my_game_pk").Debug().Where("uid=? and days=? and game_type=?", operator.Id, tdayInt, 1).Count(&sscount)
		if sscount == 0 {
			response.Error(ctx, sferrors.NewStringf("未完成任务"))
			return
		}
	case 8: //高级角斗场
		var sscount int64
		db.GetDb().Table("my_game_pk").Where("uid=? and days=? and game_type=?", operator.Id, tdayInt, 2).Count(&sscount)
		if sscount == 0 {
			response.Error(ctx, sferrors.NewStringf("未完成任务"))
			return
		}
	case 9:
		//通证解锁10
		var my_order_wCnt int64
		db.GetDb().Table("my_order_w").Debug().Where("uid=? and money=10 and status=1 and sh_time>'2025-09-16 00:00:00'", operator.Id).Count(&my_order_wCnt)
		if my_order_wCnt <= tcount {
			response.Error(ctx, sferrors.NewStringf("未完成任务"))
			return
		}
	case 10: //通证解锁50
		var my_order_wCnt int64
		db.GetDb().Table("my_order_w").Debug().Where("uid=? and money=50 and status=1 and sh_time>'2025-09-16 00:00:00'", operator.Id).Count(&my_order_wCnt)
		if my_order_wCnt <= tcount {
			response.Error(ctx, sferrors.NewStringf("未完成任务"))
			return
		}
	case 11: //通证解锁100
		var my_order_wCnt int64
		db.GetDb().Table("my_order_w").Where("uid=? and money=100 and status=1 and sh_time>'2025-09-16 00:00:00'", operator.Id).Count(&my_order_wCnt)
		if my_order_wCnt <= tcount {
			response.Error(ctx, sferrors.NewStringf("未完成任务"))
			return
		}
	case 13: //所有的每日任务
		var my_order_wCnt int64
		db.GetDb().Table("my_my_task").Where("uid=? and tid<6 and days=?", operator.Id, tday).Count(&my_order_wCnt)
		if my_order_wCnt < 4 {
			response.Error(ctx, sferrors.NewStringf("未完成任务"))
			return
		}
	case 14, 15, 16: //高级角斗场

		ss := []sssss{}
		db.GetDb().Table("my_game_moneys_tow").Select("id").Where("uid=? and days=? ", operator.Id, tdayInt).Group("game_id").Find(&ss)
		sscount := len(ss)
		if sscount < task.Num {
			response.Error(ctx, sferrors.NewStringf("未完成任务"))
			return
		}
	case 17, 18, 19: //高级角斗场
		ss := []sssss{}
		db.GetDb().Table("my_game_moneys_sl").Select("id").Where("uid=? and days=? ", operator.Id, tdayInt).Group("game_id").Find(&ss)
		sscount := len(ss)
		if sscount < (task.Num) {
			response.Error(ctx, sferrors.NewStringf("未完成任务"))
			return
		}
	case 21, 22, 23, 24: //高级角斗场
		type sssssNum struct {
			Num float64 `json:"num"`
		}
		var sscount float64
		sssssnnn := []sssssNum{}
		db.GetDb().Table("my_game_moneys_tow").Select("id,num").Where("uid=? and days=? ", operator.Id, tdayInt).Find(&sssssnnn)
		if len(sssssnnn) > 0 {
			for _, sss := range sssssnnn {
				sscount += sss.Num
			}
		}

		if sscount < float64(task.Num) {
			response.Error(ctx, sferrors.NewStringf("未完成任务"))
			return
		}
	}

	days := time.Now().AddDate(0, 0, -1).Format("20060102")
	daysInt, _ := strconv.Atoi(days)

	err = db.GetDb().Transaction(func(tx *gorm.DB) error {
		var myUser model.My_user
		txErr := tx.Select("tz", "old_tz", "id", "mobile", "sl", "fh").Where("id=?", operator.Id).First(&myUser).Error
		money := task.Jl

		if task.Id >= 25 && task.Id <= 34 {
			if myUser.OldTz < float64(task.Num) {
				return sferrors.NewStringf("任务未达标")
			}
		}

		if task.Id == 3 {
			if myUser.Fh != daysInt {
				return sferrors.NewStringf("任务未达标")
			}
		}
		txErr = service.CreateSlLog(tx, myUser, money, 3, myUser.Id, task.Desc)
		if txErr != nil {
			return txErr
		}

		newData := model.My_my_task{
			Uid:  operator.Id,
			Days: tdayInt,
			Tid:  task.Id,
		}
		txErr = tx.Create(&newData).Error
		if txErr != nil {
			return txErr
		}

		return nil
	})
	if err != nil {
		response.Error(ctx, err)
		return
	}
	response.OkData[string](ctx, "操作成功")
}

// PushTaskSong 转赠
// @Summary		转赠
// @Description	转赠
// @Tags		cms/accounts
// @Accept		json
// @Produce		json
// @Param		RequestQuery query model.QueryUserLogRequest true "Request Parameters"
// @Success		200 {object} response.Response[dto.PaginationList[emodel.SysUserLog]]
// @Router		/api/user/v2/flishs/song [POST]
func (handler *FlishHandler) PushTaskSong(ctx *gin.Context) {
	operator, err := getOperator(ctx)
	if err != nil {
		response.Error(ctx, err)
		return
	}

	id := ctx.Param("id")
	if id == "" {
		response.Error(ctx, sferrors.Code(sferrors.ErrorBadRequest))
		return
	}
	strId, err := strconv.ParseInt(id, 10, 64)
	if err != nil {
		response.Error(ctx, sferrors.New(err).SetCode(sferrors.SystemError))
		return
	}

	var tuser model.My_user
	if err = db.GetDb().Select("id", "mobile", "bio", "pid", "is_task").Where("id=?", strId).First(&tuser).Error; err != nil {
		response.Error(ctx, sferrors.NewStringf("只能送给自己下级"))
		return
	}
	if tuser.Id == 0 {
		response.Error(ctx, sferrors.Code(sferrors.ErrorBadRequest))
		return
	}
	if tuser.Pid != operator.Id {
		response.Error(ctx, sferrors.NewStringf("只能送给自己下级"))
		return
	}
	if tuser.IsTask > 0 {
		response.Error(ctx, sferrors.NewStringf("该用户已有鱼"))
		return
	}
	if tuser.Bio != "0" && tuser.Bio != "" {
		response.Error(ctx, sferrors.NewStringf("该用户已被赠送"))
		return
	}

	err = db.GetDb().Transaction(func(tx *gorm.DB) error {

		var myUser model.My_user
		txErr := tx.Select("n").Where("id=?", operator.Id).First(&myUser).Error
		if txErr != nil {
			return txErr
		}
		if myUser.N <= 0 {
			response.Error(ctx, sferrors.NewStringf("没有次数了"))
			return errors.New("没有次数了")
		}

		err = tx.Exec("update my_user set bio=1 where id=? ", strId).Error
		if err != nil {
			return err
		}

		err = tx.Exec("update my_user set n=n-1 where id=? ", operator.Id).Error
		if err != nil {
			return err
		}
		err = tx.Create(&model.MySongs{
			Uid:     operator.Id,
			Mobile:  tuser.Mobile,
			Puid:    tuser.Pid,
			Pmobile: tuser.Mobile,
		}).Error
		if err != nil {
			return err
		}
		return nil
	})
	if err != nil {
		response.Error(ctx, err)
		return
	}
	response.OkData[string](ctx, "操作成功")
}
